package cn.com.surker.system.service.impl;

import cn.com.surker.enums.StateEnum;
import cn.com.surker.system.dao.*;
import cn.com.surker.system.entity.*;
import cn.com.surker.system.model.CacheUser;
import cn.com.surker.system.service.StationLogService;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author lucky
 * @create 2022年08月08 10:40
 * @desc
 */
@Service
public class StationLogServiceImpl implements StationLogService {

    @Resource
    private SysStationLogDao mapper;
    @Resource
    private SysUserDao userDao;
    /**数据源dao**/
    @Resource
    private DataSourceInfoDao dsDao;
    @Resource
    private SysUserPositionDao userPositionDao;
    @Resource
    private SysOriginDao originDao;

    /**
     * 根据用户ID添加登录记录
     * @param cacheUser
     * @return
     */
    public CacheUser save(CacheUser cacheUser) {
        if (StringUtils.isNull(cacheUser.getId()))
            return cacheUser;

        SysUser user = userDao.selectById(cacheUser.getId());
        if (user == null || user.getStatus().intValue() == StateEnum.DISABLED.getCode())
            return cacheUser;

        List<SysUserPosition> positionList = userPositionDao.queryByUid(cacheUser.getId());
        if (positionList != null) {
            SysUserPosition position = positionList.get(0);
            SysOrigin origin = originDao.selectById(position.getOrgId());
            if (origin != null) {
                if (origin.getType() != 2) {//如果部门类型不是公司
                    origin = originDao.selectById(origin.getPid());
                    cacheUser.setOriginId(origin.getId());
                    cacheUser.setOriginName(origin.getOriginName());
                } else {
                    cacheUser.setOriginId(origin.getId());
                    cacheUser.setOriginName(origin.getOriginName());
                }
            }
        }
        /**获取选择站点日志**/
        SysStationLog sysStationLog = new SysStationLog();
        sysStationLog.setUid(user.getId());
        sysStationLog.setStatus(StateEnum.ENABLED.getCode());
        SysStationLog log = mapper.findByOne(sysStationLog);
        if (log != null) {
            DataSourceInfo ds = dsDao.queryDSByOriginId(log.getOrgId());
            if (ds != null) {
                cacheUser.setOriginId(log.getOrgId());
                cacheUser.setOriginName(log.getOrgName());
                cacheUser.setOriginFullName(log.getFullName());
                cacheUser.setDataSourceInfo(ds);

                sysStationLog.setCreateTime(new Date());
                sysStationLog.setDsId(ds.getId());
                sysStationLog.setFullName(log.getFullName());
                sysStationLog.setOrgId(log.getOrgId());
                sysStationLog.setLoginIp(cacheUser.getIp());
                sysStationLog.setName(log.getName());
                sysStationLog.setOrgName(log.getOrgName());
                sysStationLog.setPhone(cacheUser.getPhone());
                sysStationLog.setUsername(cacheUser.getUsername());
                mapper.insert(sysStationLog);
            }
        }
        return cacheUser;
    }

    /**
     *
     * @param uid
     * @return
     */
    public SysStationLog findByUid(Long uid, Integer code) {
        SysStationLog sysStationLog = new SysStationLog();
        sysStationLog.setUid(uid);
        sysStationLog.setStatus(code);
        return mapper.findByOne(sysStationLog);
    }

    /**
     * 根据条件查询所有的数据
     * @param dto
     * @return
     */
    public List<SysStationLog> findAll(SysStationLog dto) {
        QueryWrapper<SysStationLog> query = new QueryWrapper();
        query.setEntity(dto);
        List<SysStationLog> list = mapper.selectList(query);
        return list;
    }

    /**
     * 分页查询数据
     * @param dto page
     * @return
     */
    public IPage findAllByPage(SysStationLog dto, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        QueryWrapper<SysStationLog> query = new QueryWrapper();
        if (!StringUtils.isNull(dto.getUsername()))
            query.like("username", dto.getUsername());
        if (!StringUtils.isNull(dto.getName()))
            query.like("name", dto.getName());
        if (!StringUtils.isNull(dto.getOrgName()))
            query.like("org_name", dto.getOrgName());

        query.setEntity(dto);
        IPage<SysStationLog> iPage = mapper.selectPage(new Page<>((offset + limit) / limit, limit), query);
        /**分页插件**/
        if (!StringUtils.isNull(iPage.getRecords())) {
            PageInfo pageInfo = new PageInfo(iPage.getRecords());
            if (!StringUtils.isNull(pageInfo.getTotal()))
                iPage.setTotal(pageInfo.getTotal());
        }
        return iPage;
    }

    @Override
    public Integer saveInit(SysStationLog dto) {
        int saveStatus = mapper.insert(dto);
        if(saveStatus > 0){
            return 0;
        }else{
            return -1;
        }
    }

    @Override
    public Integer update(SysStationLog dto) {
        int saveStatus = mapper.updateById(dto);
        if(saveStatus > 0){
            return 0;
        }else{
            return -1;
        }
    }
}
